# resource DOM Tree generation

import xml.dom.minidom

import wxpyview.ui.core.common.manifest

class DOM:
    TYPE_APPLICATION = 0
    TYPE_MODULE = 1
    
    def __init__(self):
        self.dom = None
        self.head = None
        self.type = None

    def create(self, filename):
        self.dom = xml.dom.minidom.parse(filename)

        # check "<wx:Application> or <wx:Module>" exists, as a head of DOM.
        # "<wx:Application>" should be exist uniquely.
        
        app_heads = self.dom.getElementsByTagNameNS(wxpyview.ui.core.common.manifest.WXPYVIEW_NS, "Application")
        module_heads = self.dom.getElementsByTagNameNS(wxpyview.ui.core.common.manifest.WXPYVIEW_NS, "Module")
        
        if len(app_heads) != 0: self.head = app_heads[0]
        if len(module_heads) != 0: self.head = module_heads[0]

    def getType(self):
        types = {"wx:Application": DOM.TYPE_APPLICATION,
                 "wx:Module": DOM.TYPE_MODULE}
        return types.get(self.head.nodeName, -1)
        
    def getRootNode(self):
        return self.head